package com.faxsun.validation;

import javax.annotation.Resource;

import org.apache.commons.validator.GenericValidator;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.core.service.CustomerService;
import org.broadleafcommerce.profile.web.controller.validator.RegisterCustomerValidator;
import org.broadleafcommerce.profile.web.core.form.RegisterCustomerForm;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

public class MyRegisteratorValidator extends RegisterCustomerValidator {

	private String validatePasswordExpression = "[0-9A-Za-z@]{4,15}";

	@Resource(name = "blCustomerService")
	private CustomerService customerService;

	@SuppressWarnings({ "rawtypes" })
	public boolean supports(Class clazz) {
		return clazz.equals(RegisterCustomerForm.class);
	}

	public void validate(Object obj, Errors errors) {
		validate(obj, errors, false);
	}

	public void validate(Object obj, Errors errors, boolean useEmailForUsername) {
		RegisterCustomerForm form = (RegisterCustomerForm) obj;

		if (!GenericValidator.isEmail(form.getCustomer().getEmailAddress())) {
			errors.rejectValue("customer.emailAddress",
					"emailAddress.invalid", null, null);
			return;
		}
		
		Customer customerFromDb = customerService.readCustomerByUsername(form
				.getCustomer().getUsername(),false);

		if (customerFromDb != null) {
			if (useEmailForUsername) {
				errors.rejectValue("customer.emailAddress",
						"emailAddress.used", null, null);
			} else {
				errors.rejectValue("customer.username", "username.used", null,
						null);
			}
		}

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"password.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm",
				"passwordConfirm.required");

		errors.pushNestedPath("customer");
		// ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName",
		// "firstName.required");
		// ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName",
		// "lastName.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress",
				"emailAddress.required");
		errors.popNestedPath();

		if (!errors.hasErrors()) {

			if (!form.getPassword().matches(getValidatePasswordExpression())) {
				errors.rejectValue("password", "password.invalid", null, null);
			}

			if (!form.getPassword().equals(form.getPasswordConfirm())) {
				errors.rejectValue("password", "passwordConfirm.invalid", null,
						null);
			}

			if (!GenericValidator.isEmail(form.getCustomer().getEmailAddress())) {
				errors.rejectValue("customer.emailAddress",
						"emailAddress.invalid", null, null);
			}
		}
	}

	public String getValidatePasswordExpression() {
		return validatePasswordExpression;
	}

	public void setValidatePasswordExpression(String validatePasswordExpression) {
		this.validatePasswordExpression = validatePasswordExpression;
	}
}
